package com.cincc.cinccimdemo.ui.conversation.card;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.MyApplication;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.model.CardInfo;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.viewmodel.ContactViewModel;
import com.cincc.cinccimdemo.viewmodel.GlobalViewModel;
import com.cincc.cinccimdemo.widget.SideBarView;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.enums.ImCardType;

import java.util.ArrayList;
import java.util.List;

public class PickUserActivity extends IMBaseActivity {

    @Override
    protected int contentLayout() {
        return R.layout.activity_pick_user;
    }

    List<FriendInfoUI> list = new ArrayList<>();
    PickUserAdapter contactAdapter;
    RecyclerView recyclerView;
    SideBarView sideBarView;
    TextView mTextCenterContact;

    ContactViewModel contactViewModel;
    GlobalViewModel sequenceViewModel;

    @Override
    protected void bindViews() {
        super.bindViews();

        recyclerView = findViewById(R.id.mList_contact);
        contactAdapter = new PickUserAdapter(this, list);
        recyclerView.setAdapter(contactAdapter);
        sideBarView = findViewById(R.id.mSide_bar_contact);
        mTextCenterContact = findViewById(R.id.mText_center_contact);
        contactAdapter.setOnDataClickItem(this::openDetail);

        contactViewModel = new ViewModelProvider(this).get(ContactViewModel.class);
        contactViewModel.contactListLiveData().observe(this, friendInfos -> {
            contactAdapter.setContactList(friendInfos);
            setSideView();
        });
        MyApplication app = (MyApplication) getApplication();
        sequenceViewModel = app.getViewModel();
    }

    private void openDetail(int index) {
        FriendInfoUI item = list.get(index);
        getTicket(item);
    }

    public void setSideView() {
        // 设置需要显示的索引栏内容
        String[] letters = ContactUtil.getLetters(1, contactAdapter.getList());
        sideBarView.setLetter(letters);
        // 设置需要显示的提示框
        sideBarView.setTextView(mTextCenterContact);
        sideBarView.setOnTouchingLetterChangedListener(s -> {
            int position = ContactUtil.getLetterPosition(list, s, 1);
            if (position != -1) {
                recyclerView.scrollToPosition(position);
            }
        });
    }

    private void getTicket(FriendInfoUI friendInfoUI) {
        CardInfo cardInfo = new CardInfo();
        cardInfo.friendInfoUI = friendInfoUI;
        IMSipPhoneCtrl.getInstance().createCard(ImCardType.IM_PERSON_CARD, friendInfoUI.userId, "fengzi", imResponse -> {
            if (imResponse.getCode() == 0) {
                cardInfo.ticket = (String) imResponse.getData();
                sequenceViewModel.setCardInfo(cardInfo);
                finish();
            } else {
                runOnUiThread(() -> {
                    String data = (String) imResponse.getData();
                    Toast.makeText(PickUserActivity.this, "aaaaaaaaaaaa", Toast.LENGTH_SHORT).show();
                });
            }
        });
    }

}
